
const shell = require('./builder.shell');

const ComponentBuilder = function(){

    let _buildCompontentLaunch = function (c,source) {
        source = source.replace('/*Options*/', JSON.stringify(c.configs));
        // TODO: other handler

        return source;
    }

    let _build = function *(page) {
        let jsContent = '';
        let cssContent = '';
        let xmlContent = '';
        let launchContent = '';

        for(var i = 0; i < page.components.length; ++i){
            let c = page.components[i];
            let cFilePath = `src/components/${c.compontentCategory}/${c.compontentName}`;

            let cssFile = `${cFilePath}/${c.compontentName}.${c.themeName}.css`;
            let szCss = yield shell.exec(`cat ${cssFile}`);
            if (szCss) cssContent += szCss + '\n';

            let xmlFile = `${cFilePath}/${c.compontentName}.xml`;
            let szXml = yield shell.exec(`cat ${xmlFile}`);
            if (szXml) xmlContent += szXml + '\n';

            let jsFile = `${cFilePath}/${c.compontentName}.js`;
            let szJs = yield shell.exec(`cat ${jsFile}`);
            if (szJs) jsContent += szJs + '\n';

            let jsLaunchFile = `${cFilePath}/${c.compontentName}.launch.js`;
            let szLaunch = yield shell.exec(`cat ${jsLaunchFile}`);
            if (szLaunch) launchContent += _buildCompontentLaunch(c, szLaunch);

        }

        return {
            jsContent: jsContent,
            cssContent: cssContent,
            xmlContent: xmlContent,
            launchContent: launchContent
        }
    };

    return {
        build: _build
    }
}();

module.exports = ComponentBuilder;
